package com.expert.conrtoller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.expert.annotations.RoleAnnotation;
import com.expert.enums.ProjectStatus;
import com.expert.enums.RoleLevel;
import com.expert.pojo.Project;
import com.expert.result.Result;
import com.expert.service.ProjectService;
import com.expert.vo.ProjectInfoVo;
import com.expert.vo.ProjectOptions;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName: ProjectController
 * @Author: cxs
 * @Date: 2024/03/20 15:04
 * @Description:
 **/
@RestController
@RequestMapping("/project")
@Slf4j
public class ProjectController {
    @Autowired
    ProjectService projectService;


    /**
     * 分页查询项目基本信息
     * @param page 当前页
     * @param pageSize 每页条数
     * @param projectStatus 项目状态
     * @param projectName 项目名
     * @param beginTime 项目评审时间范围开始
     * @param endTime  项目评审时间范围结束
     * @return Result<Page<Project>>
     */

    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/pageProject")
    public Result<Page<Project>> pageProject(Integer page, Integer pageSize,
                                             Integer projectStatus, String projectName,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("分页查询项目信息");
        Page<Project> projectPage = this.projectService.pageProjectService(page, pageSize, projectStatus, projectName, beginTime, endTime);
        return Result.success(projectPage);
    }
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/pageExtractProject")
    public Result<Page<Project>> pageExtractProject(Integer page, Integer pageSize,
                                             Integer projectStatus, String projectName,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime beginTime,
                                             @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") LocalDateTime endTime) {
        log.info("分页查询项目信息");
        Page<Project> projectPage = this.projectService.pageExtractProjectService(page, pageSize, projectStatus, projectName, beginTime, endTime);
        return Result.success(projectPage);
    }
    /**
     * 添加项目信息
     * @param project 项目信息
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PostMapping("/addProject")
    public  Result addProject(@RequestBody Project project){
        log.info("登记项目：{}",project);
        this.projectService.addProject(project);
        return  Result.success();
    }

    /**
     * 根据id获取项目信息
     * @param id 项目id
     * @return Result<Project>
     */
    @RoleAnnotation(level = RoleLevel.USER)
    @GetMapping("/getById/{id}")
    public Result<Project> getProjectById(@PathVariable("id")Long id){
        log.info("根据id获取项目信息，{}",id);
        Project project=this.projectService.getProjectById(id);
        return  Result.success(project);
    }
    /**
     * 根据id获取项目完整信息
     * @param id 项目id
     * @return Result<Project>
     */
    @RoleAnnotation(level = RoleLevel.USER)
    @GetMapping("/getInfoById/{id}")
    public Result<ProjectInfoVo> getProjectInfoById(@PathVariable("id")Long id){
        log.info("根据id获取项目信息，{}",id);
        ProjectInfoVo project=this.projectService.getProjectInfoById(id);
        return  Result.success(project);
    }

    /**
     * 修改项目信息
     * @param project 项目信息
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PutMapping("/updateProject")
    public  Result updateProject(@RequestBody Project project){
        log.info("修改项目：{}",project);
        this.projectService.updateProject(project);
        return  Result.success();
    }
    /**
     * 修改项目信息
     * @param id 项目id
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @PutMapping("/cancelProject/{id}")
    public  Result cancelProject(@PathVariable Long id){
        log.info("取消项目：{}",id);
        this.projectService.cancelProject(id);
        return  Result.success();
    }

    /**
     * 获取未抽取专家的内容
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getProjectOptions")
    public Result<List<ProjectOptions>>getProjectOptions(){
        List<ProjectOptions> list=this.projectService.getProjectOptions();
        return Result.success(list);
    }
    /**
     * 获取已设置条件专家的内容
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getExtractionConditions")
    public Result<List<ProjectOptions>>getExtractCondition(){
        List<Project> list=this.projectService.lambdaQuery().eq(Project::getProjectStatus, ProjectStatus.NOT_DRAWN.getCode()).list();
        List<ProjectOptions> optionsList = list.stream().map(item -> {
            ProjectOptions projectOptions = new ProjectOptions();
            BeanUtils.copyProperties(item, projectOptions);
            return projectOptions;
        }).toList();
        return Result.success(optionsList);
    }

    /**
     * @return
     */
    @RoleAnnotation(level = RoleLevel.ADMIN)
    @GetMapping("/getProjectInfo/{id}")
    public Result<ProjectInfoVo>getProjectInfo(@PathVariable Long id){
        ProjectInfoVo info=this.projectService.getProjectInfo(id);
        return Result.success(info);
    }

}
